import {
  checkOrderSpare,
  getBeforeCheckOrderSpareDetail,
  getOrderDetailWorkSpareList,
  getOrderSpareMaterial,
  OrderSpareDetailProjectInfo,
  OrderSpareLogList,
  spareSaveOrder,
} from '@/api/parts'
import { ref } from 'vue'
import { SparePartForm } from './types'
import { getOrderSparePageSelect, getOrderSpareSpecSelect } from '@/api/order'

/**
 * 配件表单
 */
export const useSpareForm = () => {
  const formSaveLoading = ref(false)
  const form = ref<SparePartForm>({
    delSpareMaterialIds: [],
    id: '',
    networkSpareType: 1,
    receiptAddrType: 1,
    receiptAddress: '',
    receiptArea: '',
    receiptCity: '',
    receiptName: '',
    receiptPhone: '',
    receiptProvince: '',
    receiptTown: '',
    remark: '',
    spareMaterialVos: [
      {
        id: '',
        delImageIds: [],
        images: [],
        model: '',
        name: '',
        overinsured: false,
        price: 0,
        qty: 1,
        remark: '',
        spareId: '',
        specId: '',
        specName: '',
        specSku: '',
        type: 2,
        discountPrice: '100',
        applyDiscountPrice: '0',
        code: '',
        workWarrantyCategoryId: '',
        oldCopeWay: 1,
        enableBatchNo: false,
        highPricedItems: false,
        isSummary: false,
        discount: '',
        receive: '',
        materialSelectPopoverVisible: false,
      },
    ],
    type: 2,
    userOrNetworkId: '',
    workId: '',
  })

  const formSave = async (submitType: 1 | 2) => {
    formSaveLoading.value = true
    const res = await spareSaveOrder(form.value, submitType)
    formSaveLoading.value = false
    return res.data
  }

  const resetForm = () => {
    form.value = {
      delSpareMaterialIds: [],
      id: '',
      networkSpareType: 1,
      receiptAddrType: 1,
      receiptAddress: '',
      receiptArea: '',
      receiptCity: '',
      receiptName: '',
      receiptPhone: '',
      receiptProvince: '',
      receiptTown: '',
      remark: '',
      spareMaterialVos: [
        {
          id: '',
          delImageIds: [],
          images: [],
          model: '',
          name: '',
          overinsured: false,
          price: 0,
          qty: 1,
          remark: '',
          spareId: '',
          specId: '',
          specName: '',
          specSku: '',
          type: 2,
          discountPrice: '',
          applyDiscountPrice: '0',
          code: '',
          workWarrantyCategoryId: '',
          oldCopeWay: 1,
          enableBatchNo: false,
          highPricedItems: false,
          isSummary: false,
          discount: '',
          receive: '',
          materialSelectPopoverVisible: false,
        },
      ],
      type: 1,
      userOrNetworkId: '',
      workId: '',
    }
  }

  return {
    form,
    formSaveLoading,
    formSave,
    resetForm,
  }
}

/**
 * 获取工单物料下拉列表
 */
export const useGetOrderMaterialOptionList = () => {
  const getOptionListLoading = ref(false)
  const getOptionListParam = ref({
    limit: 50,
    name: '',
    page: 1,
    id: '',
  })
  const optionList = ref<Array<OrderMaterialOption>>([])

  const getOptionList = async (orderId: string) => {
    getOptionListLoading.value = true
    const res = await getOrderSparePageSelect(getOptionListParam.value, orderId)
    optionList.value = res.data.data.rows
    getOptionListLoading.value = false
  }

  return {
    optionList,
    getOptionListParam,
    getOptionListLoading,
    getOptionList,
  }
}

/**
 * 获取物料规格下拉选项
 */
export const useGetOrderMaterialSpecOptionList = () => {
  const getOptionListLoading = ref(false)
  const getOptionListParam = ref({
    limit: 50,
    name: '',
    page: 1,
  })
  const optionList = ref<Array<OrderMaterialSpecOption>>([])

  const getOptionList = async (materialId: string) => {
    getOptionListLoading.value = true
    const res = await getOrderSpareSpecSelect(getOptionListParam.value, materialId)
    optionList.value = res.data.data.rows
    getOptionListLoading.value = false
  }

  return {
    optionList,
    getOptionListParam,
    getOptionListLoading,
    getOptionList,
  }
}

/**
 * 获取物料单详情
 */
export const useGetMaterialDetail = () => {
  const getDetailLoading = ref(false)
  const detail = ref<OrderMaterialDetail>({
    applyEmp: '',
    applyEmpType: 1,
    applyRemark: '',
    code: '',
    createTime: '',
    id: '',
    networkSpareType: 1,
    oldCopeWay: 1,
    oldReceiptAddress: null,
    oldReceiptArea: null,
    oldReceiptCity: null,
    oldReceiptName: null,
    oldReceiptPhone: null,
    oldReceiptProvince: null,
    oldReceiptTown: null,
    receiptAddrType: 1,
    receiptAddress: '',
    receiptArea: '',
    receiptCity: '',
    receiptName: '',
    receiptPhone: '',
    receiptProvince: '',
    receiptTown: '',
    received: true,
    reviewEmp: '',
    reviewRemark: '',
    reviewTime: null,
    status: 1,
    store: null,
    type: 1,
    updateTime: '',
    userOrNetworkId: '',
    workId: '',
    workNo: '',
  })

  const getDetail = async (id: string) => {
    getDetailLoading.value = true
    const res = await OrderSpareDetailProjectInfo(id)
    detail.value = res.data.data.row
    getDetailLoading.value = false
  }

  return {
    detail,
    getDetailLoading,
    getDetail,
  }
}

/**
 * 获取物料详情的物料列表
 */
export const useGetMaterialDetailSparePartList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 列表
  const list = ref<Array<OrderMaterialDetailSparePart>>([])

  /**
   * 获取列表
   * @param {string} id - 物料单id
   */
  const getList = async (id: string) => {
    getListLoading.value = true
    const res = await getOrderSpareMaterial(id)
    list.value = res.data.data.rows
    getListLoading.value = false
  }

  return {
    list,
    getListLoading,
    getList,
  }
}

/**
 * 获取物料操作日志
 */
export const useGetMaterialOperateLog = () => {
  const getLogLoading = ref(false)
  const getLogParam = ref({
    limit: 50,
    page: 1,
  })
  const log = ref<Array<OrderMaterialOperateLog>>([])
  const listTotal = ref(0)

  const getLog = async (materialId: string) => {
    getLogLoading.value = true
    const res = await OrderSpareLogList(getLogParam.value, materialId)
    log.value = res.data.data.rows
    listTotal.value = Number(res.data.data.total)
    getLogLoading.value = false
  }

  return {
    log,
    listTotal,
    getLogParam,
    getLogLoading,
    getLog,
  }
}

/**
 * 根据工单id获取物料列表
 */
export const useGetMaterialListByOrderId = () => {
  const getListLoading = ref(false)
  const getListParam = ref({
    limit: 1000,
    page: 1,
  })
  const list = ref<Array<OrderMaterial>>([])
  const listTotal = ref(0)

  const getList = async (orderId: string) => {
    getListLoading.value = true
    const res = await getOrderDetailWorkSpareList(getListParam.value, orderId)
    list.value = res.data.data.rows
    listTotal.value = Number(res.data.data.total)
    getListLoading.value = false
  }

  return {
    list,
    listTotal,
    getListParam,
    getListLoading,
    getList,
  }
}

/**
 * 审核配件
 */
export const useAuditSparePart = () => {
  const formSaveLoading = ref(false)
  const form = ref<OrderAPIRequest.AuditSparePart>({
    id: '',
    oldCopeWay: 2,
    oldReceiptAddress: '',
    oldReceiptArea: '',
    oldReceiptCity: '',
    oldReceiptName: '',
    oldReceiptPhone: '',
    oldReceiptProvince: '',
    oldReceiptTown: '',
    receiptAddrType: 1,
    receiptAddress: '',
    receiptArea: '',
    receiptCity: '',
    receiptName: '',
    receiptPhone: '',
    receiptProvince: '',
    receiptTown: '',
    remark: '',
    storeId: '',
    storePositionId: '',
    userOrNetworkId: '',
    workSpareMaterials: [],
  })

  const resetForm = () => {
    form.value = {
      id: '',
      oldCopeWay: 2,
      oldReceiptAddress: '',
      oldReceiptArea: '',
      oldReceiptCity: '',
      oldReceiptName: '',
      oldReceiptPhone: '',
      oldReceiptProvince: '',
      oldReceiptTown: '',
      receiptAddrType: 1,
      receiptAddress: '',
      receiptArea: '',
      receiptCity: '',
      receiptName: '',
      receiptPhone: '',
      receiptProvince: '',
      receiptTown: '',
      remark: '',
      storeId: '',
      storePositionId: '',
      userOrNetworkId: '',
      workSpareMaterials: [],
    }
  }

  const submit = async () => {
    formSaveLoading.value = true
    const res = await checkOrderSpare(form.value)
    formSaveLoading.value = false
    return res.data
  }

  return {
    form,
    formSaveLoading,
    submit,
    resetForm,
  }
}

/**
 * 获取配件审核之前的信息
 */
export const useGetSparePartInfoBeforeAudit = () => {
  const getInfoLoading = ref(false)
  const info = ref<OrderSparePartInfoBeforeAudit>({
    applyEmp: '',
    applyRemark: '',
    createTime: '',
    id: '',
    oldCopeWay: 0,
    receiptAddrType: 2,
    receiptAddress: '',
    receiptArea: '',
    receiptCity: '',
    receiptName: '',
    receiptPhone: '',
    receiptProvince: '',
    receiptTown: '',
    code: '',
    applyEmpType: 1,
    networkSpareType: 1,
    received: false,
    reviewEmp: '',
    reviewRemark: '',
    reviewTime: '',
    status: 1,
    type: 1,
    updateTime: '',
    userOrNetworkId: '',
    workId: '',
    workNo: '',
  })

  const getInfo = async (id: string) => {
    getInfoLoading.value = true
    const res = await getBeforeCheckOrderSpareDetail(id)
    info.value = res.data.data.row
    getInfoLoading.value = false
  }

  return {
    info,
    getInfoLoading,
    getInfo,
  }
}
